package com.winter.service;

import com.google.common.base.Strings;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.winter.mapper.CaBondBaseinfoMoneyMapper;
import com.winter.mapper.CaBondCrPriValuMapper;
import com.winter.mapper.CaBondCredAttMoneyMapper;
import com.winter.mapper.CaBondCredChanMoneyMapper;
import com.winter.mapper.CaBondCredMoneyMapper;
import com.winter.mapper.CaBondIssCredMoneyMapper;
import com.winter.mapper.CaBondNewIssMoneyHisMapper;
import com.winter.mapper.CaBondNewIssMoneyMapper;
import com.winter.mapper.CaCpMapper;
import com.winter.mapper.CaShiborMoneyMapper;
import com.winter.mapper.DictEntryMapper;
import com.winter.model.CaBondCrPriValu;
import com.winter.model.CaBondCredAttMoney;
import com.winter.model.CaBondCredChanMoney;
import com.winter.model.CaBondCredMoney;
import com.winter.model.CaBondIssCredMoney;
import com.winter.model.CaBondNewIssMoney;
import com.winter.model.CaBondNewIssMoneyHis;
import com.winter.model.CaCp;
import com.winter.model.CaShiborMoney;
import com.winter.model.DictEntry;
import com.winter.model.IcompanyCard;
import com.winter.model.vo.response.BondBaseinfoMoneyResponseVo;
import com.winter.util.Constants;
import com.winter.util.Result;
import com.winter.util.ResultCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Service
@Transactional
public class FinancialService {

    /**
     * 债券估值
     */
    @Autowired
    public CaBondCrPriValuMapper caBondCrPriValuMapper;

    /**
     * shibor报价
     */
    @Autowired
    public CaShiborMoneyMapper caShiborMoneyMapper;

    /**
     * 新债发行
     */
    @Autowired
    private CaBondNewIssMoneyMapper caBondNewIssMoneyMapper;
    /**
     * 历史发行
     */
    @Autowired
    private CaBondNewIssMoneyHisMapper caBondNewIssMoneyHisMapper;

    /**
     * 票据报价
     */
    @Autowired
    private CaCpMapper caCpMapper;

    /**
     * 发行文件
     */
    @Autowired
    private CaBondCredAttMoneyMapper caBondCredAttMoneyMapper;

    @Autowired
    private CaBondCredMoneyMapper caBondCredMoneyMapper;

    @Autowired
    private CaBondCredChanMoneyMapper caBondCredChanMoneyMapper;

    @Autowired
    private CaBondIssCredMoneyMapper caBondIssCredMoneyMapper;

    @Autowired
    private CaBondBaseinfoMoneyMapper caBondBaseinfoMoneyMapper;
    
    @Autowired
    private DictEntryMapper dictEntryMapper;

    /**
     * 债券估值
     */
    public Result bondList(String date, Integer pageSize, Integer pageNumber) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }

        PageHelper.startPage(pageNumber, pageSize, false);
        List<CaBondCrPriValu> caBondCrPriValus = caBondCrPriValuMapper.bondList(date);
        for (int i = 0; i < caBondCrPriValus.size(); i++) {
        	getTypeName(caBondCrPriValus.get(i));
		}
        PageInfo<CaBondCrPriValu> pageInfo = new PageInfo<>(caBondCrPriValus);
        return Result.success(pageInfo);
    }
    
    public void getTypeName(CaBondCrPriValu priValu) {
    	 DictEntry dictEntry = dictEntryMapper.get("DC_BOND_VALUE_CRED_LEVEL", priValu.getCredLevel().toString());
         if (dictEntry != null) {
        	 priValu.setCredLevelName(dictEntry.getDictname());
         }
    }

    /**
     * shibor报价
     */
    public Result shiborList(String date, Integer pageSize, Integer pageNumber) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (Strings.isNullOrEmpty(date)) {
            return Result.success("时间不能为空");
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<CaShiborMoney> caShiborMonies = caShiborMoneyMapper.shiborList(date);
        PageInfo<CaShiborMoney> pageInfo = new PageInfo<>(caShiborMonies);
        return Result.success(pageInfo);
    }

    /**
     * 新债发行
     */
    public Result newIssueList(String classId, String way, String rate, String date, Integer pageSize, Integer pageNumber, String key) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<CaBondNewIssMoney> caBondNewIssMonies = caBondNewIssMoneyMapper.newIssueList(classId, way, rate, date, key);
        PageInfo<CaBondNewIssMoney> pageInfo = new PageInfo<>(caBondNewIssMonies);
        return Result.success(pageInfo);

    }

    /**
     * 历史发行
     */
    public Result historyIssue(String classId, String way, String rate, String date, Integer pageSize, Integer pageNumber, String keyword) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }

        PageHelper.startPage(pageNumber, pageSize, false);
        List<CaBondNewIssMoneyHis> caBondNewIssMoneyHis = caBondNewIssMoneyHisMapper.historyIssueList(classId, way, rate, date,keyword);
        PageInfo<CaBondNewIssMoneyHis> pageInfo = new PageInfo<>(caBondNewIssMoneyHis);
        return Result.success(pageInfo);
    }

    /**
     * 票据报价
     */
    public Result faresQuote(Integer pageSize, Integer pageNumber, String date) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        if (Strings.isNullOrEmpty(date)) {
            return Result.success("时间不能为空");
        }
        PageHelper.startPage(pageNumber, pageSize, false);
        List<CaCp> caCps = caCpMapper.faresQuoteList(date);
        PageInfo<CaCp> pageInfo = new PageInfo<>(caCps);
        return Result.success(pageInfo);
    }

    /**
     * 票据报价视图
     */
    public Result faresQuoteView(String beginDate, String endTime, String[] deadline, String date) {
        if (Strings.isNullOrEmpty(beginDate)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择起始日期");
        }
        if (Strings.isNullOrEmpty(endTime)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择截止日期");
        }
        if (deadline == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择评级");
        }
        if (date == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择期限");
        }
        //caCpMapper.faresQuoteView(deadline, date);
        return Result.success();
    }

    /**
     * 发行文件
     */
    public Result issueFileList(String rateId, String userId) {
        if (Strings.isNullOrEmpty(rateId)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (Strings.isNullOrEmpty(userId)) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        List<CaBondCredAttMoney> caBondCredAttMonies = caBondCredAttMoneyMapper.issueFileList(rateId);
        return Result.success(caBondCredAttMonies);
    }


    /**
     * 债券估值视图
     */
    public Result bondView(String beginDate, String endTime, Long[] ids, Integer deadline) {
        if (Strings.isNullOrEmpty(beginDate)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择起始日期");
        }
        if (Strings.isNullOrEmpty(endTime)) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择截止日期");
        }
        if (ids == null || ids.length == 0) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择评级");
        }
        if (deadline == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择期限");
        }
        List<CaBondCrPriValu> caBondCrPri = caBondCrPriValuMapper.bondView(beginDate, endTime, ids, deadline);
        return Result.success(caBondCrPri);
    }

    /**
     * shibor视图
     */
    public Result shiborList(String deadline[], String date) {
        if (deadline == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择期限");
        }
        if (date == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK, "请选择缩放");
        }
        List<CaShiborMoney> caShiborMonies = caShiborMoneyMapper.shiborView(deadline, date);
        return Result.success(caShiborMonies);
    }

    public Result ratingChange(Integer pageSize, Integer pageNumber, String change, String zhaiXiang, String zhuTi, String type, String date, String key) {
        if (pageSize == null) {
            pageSize = Constants.PAGESIZE;
        }
        if (pageNumber == null) {
            pageNumber = Constants.PAGENUMBER;
        }
        PageHelper.startPage(pageNumber, pageSize, false);

        List<CaBondCredMoney> caBondCredMonies = caBondCredMoneyMapper.ratingChange(change, zhaiXiang, zhuTi, type, date, key);
        if (caBondCredMonies.size() > 0) {
            caBondCredMonies.forEach(f -> {
                //获取主体评级的 评级变动 和 上次评级信息
                String change1 = "";
                String change2 = "";
                String issCredCode = f.getIssCredCode();
                if (!Strings.isNullOrEmpty(change) && change.contains("主体")) {
                    change1 = change.replace("主体", "");
                }
                List<CaBondIssCredMoney> issList = caBondIssCredMoneyMapper.queryBondIssCredMoneyChangeDirecInfo(issCredCode, zhuTi, change1);
                if (issList != null && issList.size() > 0) {
                    CaBondIssCredMoney caBondIssCredMoney = issList.get(0);
                    String credOrie = caBondIssCredMoney.getCredOrie();
                    f.setNewestIssOrie(credOrie);
                    if (issList.size() > 1) {
                        CaBondIssCredMoney caBondIssCredMoney1 = issList.get(1);
                        String issCredLevel = caBondIssCredMoney1.getIssCredLevel();
                        f.setLastTimeIssLevel(issCredLevel);
                    }
                }
                if (!Strings.isNullOrEmpty(change) && change.contains("债项")) {
                    change2 = change.replace("债项", "");
                }
                //获取债项评级的 评级变动 和 上次评级信息
                String bondCode = f.getBondCode();
                List<CaBondCredChanMoney> bondList = caBondCredChanMoneyMapper.queryBondCredChanMoneyChangeDirecInfo(bondCode, change2, zhaiXiang);
                if (bondList != null && bondList.size() > 0) {
                    CaBondCredChanMoney caBondCredChanMoney = bondList.get(0);
                    String credOrie = caBondCredChanMoney.getCredOrie();
                    f.setNewestCredOrie(credOrie);
                    if (bondList.size() > 1) {
                        CaBondCredChanMoney caBondCredChanMoney1 = bondList.get(1);
                        String bondCredLevel = caBondCredChanMoney1.getBondCredLevel();
                        f.setLastTimeCredLevel(bondCredLevel);
                    }
                }
            });
        }
        PageInfo<CaBondCredMoney> pageInfo = new PageInfo<>(caBondCredMonies);
        return Result.success(pageInfo);
    }


    public Result ratingChangeDetail(Long userId, String issCredCode, String bondCode, String type) {
        if (userId == null) {
            return Result.failure(ResultCode.USER_NOT_LOGGED_IN);
        }
        BondBaseinfoMoneyResponseVo bondInfo = caBondBaseinfoMoneyMapper.queryBondBaseinfoMoneyList(bondCode);
        if (bondInfo != null) {
            // 获取发行文件
            List<CaBondCredAttMoney> attaListByCond = caBondCredAttMoneyMapper.getAttaListByCond(bondCode);
            bondInfo.setReleaseFile(attaListByCond);
            List<CaBondIssCredMoney> caBondIssCredMonies = caBondIssCredMoneyMapper.queryBondIssCredMoneyListAll(issCredCode);
            bondInfo.setMainList(caBondIssCredMonies);
            List<CaBondCredChanMoney> bondCredChanMonies = caBondCredChanMoneyMapper.queryBondCredChanMoneyListAll(bondCode);
            bondInfo.setIndebList(bondCredChanMonies);
        }
        return Result.success(bondInfo);
    }


}
